package com.edu.zju.scs.panacea.controller.resource;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.plan.Seller;
import com.edu.zju.scs.panacea.entity.resource.Package;
import com.edu.zju.scs.panacea.entity.resource.Site;
import com.edu.zju.scs.panacea.service.resource.PackageService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-27 17:50
 */
@Controller
@EnableAutoConfiguration
public class PackageController {
    @Autowired
    private PackageService packageService;


    @ResponseBody
    @ApiOperation(value="新增资源包", notes="新增资源包")
    @RequestMapping(value = "/package/new", method = RequestMethod.POST)
    public BaseResult addPackage(
            @ApiParam("资源包命名") @RequestParam(value = "name") String name,
            @ApiParam("资源位ID") @RequestParam(value = "locIds") List<Long> locIds,
            HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        Package resPackage = new Package();
        resPackage.setName(name);
        resPackage.setSellerId(seller.getId());
        resPackage.setLocIds(locIds);
        return packageService.addPackage(resPackage);
    }

    @ResponseBody
    @ApiOperation(value="根据ID删除资源包", notes="根据ID删除资源包")
    @RequestMapping(value = "/package/deletion/{id}", method = RequestMethod.GET)
    public BaseResult deletePackage(@PathVariable Long id) {
        return packageService.deletePackage(id);
    }


    @ResponseBody
    @ApiOperation(value="查询某个商家的所有资源包", notes="查询某个商家的所有资源包")
    @RequestMapping(value = "/package/seller", method = RequestMethod.GET)
    public BaseResult getPackageBySeller(HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        return packageService.getPackagesBySeller(seller.getId());
    }

    @ResponseBody
    @ApiOperation(value="查询指定资源包或资源位下的可用资源位个数", notes="查询指定资源包或资源位下的可用资源位个数")
    @RequestMapping(value = "/package/locs", method = RequestMethod.POST)
    public BaseResult getAvailableLocs(
            @ApiParam("资源包ID") @RequestParam(value = "packageIds", required = false) List<Long> packageIds,
            @ApiParam("资源位ID") @RequestParam(value = "locIdList", required = false) List<Long> locIds,
            @ApiParam("模板类型") @RequestParam(value = "type", required = false) Integer type) {
        return packageService.getAvalibleLocs(packageIds, locIds, type);
    }
}
